﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Alogient.Cameleon.Sdk
{
    /// <summary>
    /// Contains static methods for ID and Culture retrieval from url or configuration file
    /// </summary>
    public sealed class NavigationController
    {
        /// <summary>
        /// Singleton
        /// </summary>
        private NavigationController()
        {
        }

        /// <summary>
        /// Get current page NavID
        /// </summary>
        /// <returns>NavID from URL or configuration file</returns>
        public static int GetNavID()
        {
            if (HttpContext.Current == null)
            {
                return CMSettings.DefaultNavID;
            }

            try
            {
                if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["NavID"]))
                {
                    return Convert.ToInt32(HttpContext.Current.Request.QueryString["NavID"]);
                }

                // Supports old nav_id request parameter
                if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["nav_id"]))
                {
                    return Convert.ToInt32(HttpContext.Current.Request.QueryString["nav_id"]);
                }
            }
            catch (Exception ex)
            {
                throw new CMException("NavID must be an integer", ex);
            }

            return CMSettings.DefaultNavID;
        }

        /// <summary>
        /// Get current culture code
        /// </summary>
        /// <returns>CultureCode from URL or configuration file</returns>
        public static string GetCultureCode()
        {
            if (HttpContext.Current == null)
            {
                return CMSettings.DefaultCultureCode;
            }

            if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["CultureCode"]))
            {
                return HttpContext.Current.Request.QueryString["CultureCode"];
            }

            if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["lang_id"]))
            {
                string lang = HttpContext.Current.Request.QueryString["lang_id"].ToLower();

                switch (lang)
                {
                    case "f":
                        return "fr-CA";
                    case "e":
                        return "en-CA";
                }
            }

            return CMSettings.DefaultCultureCode;
        }
    }
}
